package com.zd.lwsp.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.zd.lwsp.vo.AppInfoProcessVo;
import com.zd.lwsq.domain.ApplicationInfo;
import com.zd.lwsq.service.ApplicationInfoService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.lwsp.mapper.AppInfoProcessMapper;
import com.zd.lwsp.domain.AppInfoProcess;
import com.zd.lwsp.service.AppInfoProcessService;
import com.ruoyi.common.core.text.Convert;

/**
 * 离疆申请流程状态对象Service业务层处理
 * 
 * @author fxiao
 * @date 2020-08-08
 */
@Service
public class AppInfoProcessServiceImpl implements AppInfoProcessService
{
    @Autowired
    private AppInfoProcessMapper appInfoProcessMapper;
    @Autowired
    private ISysRoleService roleServer;
    @Autowired
    private ApplicationInfoService applicationInfoService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询离疆申请流程状态对象
     * 
     * @param id 离疆申请流程状态对象ID
     * @return 离疆申请流程状态对象
     */
    @Override
    public AppInfoProcess selectAppInfoProcessById(String id)
    {
        return appInfoProcessMapper.selectAppInfoProcessById(id);
    }

    /**
     * 查询离疆申请流程状态对象列表
     * 
     * @param appInfoProcess 离疆申请流程状态对象
     * @return 离疆申请流程状态对象
     */
    @Override
    public List<AppInfoProcess> selectAppInfoProcessList(AppInfoProcess appInfoProcess)
    {
        return appInfoProcessMapper.selectAppInfoProcessList(appInfoProcess);
    }

    @Override
    public List<AppInfoProcessVo> selectAppInfoProcessListForRole(AppInfoProcess appInfoProcess) {
        /*//暂时先不做筛选，后面再作，目前全放开
        List<AppInfoProcessVo>result=new ArrayList<>();
        List<AppInfoProcess> appInfoProcesses=appInfoProcessMapper.selectAppInfoProcessList(appInfoProcess);
        for (int i = 0; i < appInfoProcesses.size(); i++) {
            AppInfoProcessVo appInfoProcessVo=new AppInfoProcessVo();
            try {
                BeanUtils.copyProperties(appInfoProcessVo,appInfoProcesses.get(i));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            String ywdxId=appInfoProcesses.get(i).getYwdxId();
            ApplicationInfo applicationInfo=applicationInfoService.selectApplicationInfoById(ywdxId);
            appInfoProcessVo.setApplicationInfo(applicationInfo);
            result.add(appInfoProcessVo);
        }
        return result;*/

        List<AppInfoProcessVo>result=new ArrayList<>();
        //先判断当前用户的角色
        SysUser sysUser=ShiroUtils.getSysUser();
        Set<String> roles=roleServer.selectRoleKeys(sysUser.getUserId());
        boolean sj=false,qj=false,jdb=false,sq=false;
        if(roles!=null){
            Iterator<String> iterator=roles.iterator();
            while (iterator.hasNext()){
                String roleKey=iterator.next();
                switch (roleKey){
                    case "sj_admin":
                        sj=true;
                        break;
                    case "qj_admin":
                        qj=true;
                        break;
                    case "jdb_admin":
                        jdb=true;
                        break;
                    case "sq_admin":
                        sq=true;
                        break;
                }
                if(sj||qj||jdb||sq){
                    break;
                }
            }
        }
        //如果含有这几种权限
        if(sj||qj||jdb||sq){
            //只进其中一个
            if(sj){
                //拥有市级管理员角色（暂时以市级为最大了，市级就是获取全部的数据）
                List<AppInfoProcess> appInfoProcesses=appInfoProcessMapper.selectAppInfoProcessList(appInfoProcess);
                for (int i = 0; i < appInfoProcesses.size(); i++) {
                    AppInfoProcessVo appInfoProcessVo=new AppInfoProcessVo();
                    try {
                        BeanUtils.copyProperties(appInfoProcessVo,appInfoProcesses.get(i));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    String ywdxId=appInfoProcesses.get(i).getYwdxId();
                    ApplicationInfo applicationInfo=applicationInfoService.selectApplicationInfoById(ywdxId);
                    appInfoProcessVo.setApplicationInfo(applicationInfo);
                    result.add(appInfoProcessVo);
                }
            }else if(qj){
                //拥有区级管理员角色
                SysDept dept=deptService.selectDeptById(sysUser.getDeptId());
                //获取用户所属街道办
                SysDept jdbDept=deptService.selectDeptById(dept.getParentId());
                //获取用户所属区级单位
                SysDept qjDept=deptService.selectDeptById(jdbDept.getParentId());
                ApplicationInfo param=new ApplicationInfo();
                param.setQjCode(qjDept.getDeptId().toString());
                List<ApplicationInfo>ais=applicationInfoService.selectApplicationInfoList(param);
                getAppInfoPoVoListByAppliInfo(ais,result);

            }else if(jdb){
                //拥有街道办管理员角色
                SysDept sqDept=deptService.selectDeptById(sysUser.getDeptId());
                SysDept jdbDept=deptService.selectDeptById(sqDept.getParentId());
                ApplicationInfo param=new ApplicationInfo();
                param.setJdbCode(jdbDept.getDeptId().toString());
                List<ApplicationInfo> applicationInfos = applicationInfoService.selectApplicationInfoList(param);
                getAppInfoPoVoListByAppliInfo(applicationInfos,result);
            }else if(sq){
                //拥有社区管理员角色
                ApplicationInfo param=new ApplicationInfo();
                param.setSqCode(sysUser.getDeptId().toString());
                List<ApplicationInfo> applicationInfos = applicationInfoService.selectApplicationInfoList(param);
                getAppInfoPoVoListByAppliInfo(applicationInfos,result);
            }
        }
        return result;
    }

    /**
     * 根据申请对象列表封装审批对象列表
     * @param applicationInfos
     * @param result
     */
    private void getAppInfoPoVoListByAppliInfo
            (List<ApplicationInfo> applicationInfos,List<AppInfoProcessVo> result){
        for (int i = 0; i < applicationInfos.size(); i++) {
            AppInfoProcess aps=appInfoProcessMapper.selectByBusId(applicationInfos.get(i).getId());
            AppInfoProcessVo appInfoProcessVo=new AppInfoProcessVo();
            try {
                BeanUtils.copyProperties(appInfoProcessVo,aps);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            appInfoProcessVo.setApplicationInfo(applicationInfos.get(i));
            result.add(appInfoProcessVo);
        }
    }

    /**
     * 新增离疆申请流程状态对象
     * 
     * @param appInfoProcess 离疆申请流程状态对象
     * @return 结果
     */
    @Override
    public int insertAppInfoProcess(AppInfoProcess appInfoProcess)
    {
        appInfoProcess.setCreateTime(DateUtils.getNowDate());
        return appInfoProcessMapper.insertAppInfoProcess(appInfoProcess);
    }

    /**
     * 修改离疆申请流程状态对象
     * 
     * @param appInfoProcess 离疆申请流程状态对象
     * @return 结果
     */
    @Override
    public int updateAppInfoProcess(AppInfoProcess appInfoProcess)
    {
        SysUser sysUser=ShiroUtils.getSysUser();
        appInfoProcess.setUpdateTime(DateUtils.getNowDate());
        appInfoProcess.setSpsj(appInfoProcess.getUpdateTime());
        appInfoProcess.setLastUpdateBy(sysUser.getUserId().toString());
        appInfoProcess.setSprId(sysUser.getUserId().toString());
        appInfoProcess.setSprMc(sysUser.getUserName());
        return appInfoProcessMapper.updateAppInfoProcess(appInfoProcess);
    }

    /**
     * 删除离疆申请流程状态对象对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAppInfoProcessByIds(String ids)
    {
        return appInfoProcessMapper.deleteAppInfoProcessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除离疆申请流程状态对象信息
     * 
     * @param id 离疆申请流程状态对象ID
     * @return 结果
     */
    @Override
    public int deleteAppInfoProcessById(String id)
    {
        return appInfoProcessMapper.deleteAppInfoProcessById(id);
    }
}
